﻿using System.Collections;
using System.IO;
using UnityEngine;

namespace Assets.Scripts.System
{
    /// <summary>
    /// 存储信息类
    /// </summary>
    public class StorageInfo
    {
        public float BGMVol;
        public float SoundVol;
        public KeyCode attackKeyCode;
        public KeyCode interactKeyCode;
        public KeyCode switchWeaponKeyCode;
        public KeyCode leaveOrPickUpKeyCode;
        public KeyCode reloadKeyCode;
        public KeyCode dodgeKeyCode;
        public KeyCode weaponSkillKeyCode;
        public KeyCode personSkillKeyCode;
        public StorageInfo()
        {
            BGMVol = 1.0f;
            SoundVol = 1.0f;
            attackKeyCode = KeyCode.Mouse0; // 攻击
            interactKeyCode = KeyCode.E; // 交互
            switchWeaponKeyCode = KeyCode.Q; // 换武器切换
            leaveOrPickUpKeyCode = KeyCode.G; // 丢弃
            reloadKeyCode = KeyCode.R; // 换弹
            dodgeKeyCode = KeyCode.LeftShift; // 闪避
            weaponSkillKeyCode = KeyCode.Mouse1; // 武器技能
            personSkillKeyCode = KeyCode.F; // 人物技能
        }
    }
    /// <summary>
    /// 存储系统
    /// TODO：保存信息加密解密
    /// </summary>
    public class StorageSystem : MonoSingleton<StorageSystem>, IBaseSystem
    {
        public StorageInfo storageInfo;
        private string storagePath = "SaveInfo.Save";
        public void Load()
        {
            if (File.Exists($"{Application.dataPath}/{storagePath}"))
            {
                StreamReader sr = new StreamReader($"{Application.dataPath}/{storagePath}");
                string loadStr = sr.ReadToEnd();
                sr.Close();
                if (loadStr.Length > 0)
                {
                    storageInfo = JsonUtility.FromJson<StorageInfo>(loadStr);
                }
                else
                {
                    storageInfo = new StorageInfo();
                }
            }
            else
            {
                storageInfo = new StorageInfo();
            }
            Debug.Log("加载完毕！");
        }
        public void Save()
        {
            //if (!File.Exists(Application.dataPath + storagePath))
            //{
            //    File.WriteAllText(Application.dataPath + storagePath, null);
            //}
            StreamWriter sw = new StreamWriter($"{Application.dataPath}/{storagePath}");
            string saveStr = JsonUtility.ToJson(storageInfo);
            sw.WriteLine(saveStr);
            sw.Close();
            Debug.Log("保存完毕！");
        }
        public void Open()
        {
            Load();
        }
        public void OnUpdate()
        {

        }
        public void Close()
        {

        }
    }
}